﻿using FineUI;
using System;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;

namespace ZHDJ.BranchCenter.Module.TuShuo
{
    public partial class TuShuoEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyBranchPictureShow pic = DB.PartyBranchPictureShows.Find(id);
                if (pic == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                hdFileId.Text = pic.ImageID;
                if (!string.IsNullOrEmpty(hdFileId.Text))
                {
                    filePhoto.EmptyText = UpFileBusiness.GetFileName(hdFileId.Text);
                    HyperLink1.ImageID = hdFileId.Text;
                }
                txtDescription.Text = pic.Describe;
            }
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;
            PartyBranchPictureShow pic;

            if (id != "")
            {
                //更新
                pic = DB.PartyBranchPictureShows.Find(id);
                if (pic == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                DB.SaveChanges();
            }
            else
            {
                pic = new PartyBranchPictureShow();
                pic.ID = StringHelper.NewID;
                pic.BranchID = this.BranchID;
                pic.CreateTime = DateTime.Now;
                pic.CreateBy = CurrentUser.LoginID;

                DB.PartyBranchPictureShows.Add(pic);
            }

            if (!string.IsNullOrEmpty(hdFileId.Text))
            {
                pic.ImageID = hdFileId.Text;
            }
            pic.Describe = txtDescription.Text.Trim();
            pic.ModifyBy = CurrentUser.LoginID;
            pic.ModifyTime = DateTime.Now;

            DB.SaveChanges();
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            var rs = UpFileBusiness.Upload(filePhoto, CurrentUser.LoginID);
            if (rs.IsSuccess)
            {
                hdFileId.Text = rs.FileID;
                HyperLink1.ImageID = rs.FileID;
            }
        }
        #endregion
    }
}